package com.robot.member.service;

import com.robot.member.dao.EvaluateDao;
import com.robot.member.pojo.Evaluate;
import com.robot.member.pojo.client.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * evaluate服务层
 *
 * @author wuyunbin
 */
@Service
public class EvaluateService {
    @Autowired
    private EvaluateDao evaluateDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<Evaluate> findAll() {
        return evaluateDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Evaluate> findSearch(Map whereMap, int page, int size) {
        Specification<Evaluate> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return evaluateDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<Evaluate> findSearch(Map whereMap) {
        Specification<Evaluate> specification = createSpecification(whereMap);
        return evaluateDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public Evaluate findById(String id) {
        return evaluateDao.findById(id).get();
    }

    /**
     * 增加
     *
     * @param evaluate
     */
    public void add(Evaluate evaluate) {
        //雪花分布式ID生成器
        evaluate.setId(idWorker.nextId() + "");
        evaluateDao.save(evaluate);
    }

    /**
     * 增加
     *
     * @param evaluate
     */
    public void add(Evaluate evaluate, Orders orders) {
        //雪花分布式ID生成器
        evaluate.setId(idWorker.nextId() + "");
        evaluate.setMemberId(orders.getMemberId());
        evaluate.setCompanyId(orders.getCompanyId());
        evaluate.setStoreId(orders.getStoreId());
        evaluate.setStoreName(orders.getStoreName());
        evaluate.setOrdersId(orders.getId());
        LocalDateTime now=LocalDateTime.now();
        evaluate.setCreatedAt(now);
        evaluate.setUpdatedAt(now);
        evaluateDao.save(evaluate);
    }


    /**
     * 修改
     *
     * @param evaluate
     */
    public void update(Evaluate evaluate) {
        evaluateDao.save(evaluate);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        evaluateDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Evaluate> createSpecification(Map searchMap) {
        return new Specification<Evaluate>() {
            @Override
            public Predicate toPredicate(Root<Evaluate> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // 评价表id
                String id = "id";
                if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                    predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
                }
                // 会员id
                String memberId = "memberId";
                if (searchMap.get(memberId) != null && !"".equals(searchMap.get(memberId))) {
                    predicateList.add(cb.like(root.get(memberId).as(String.class), "%" + (String) searchMap.get(memberId) + "%"));
                }
                // 商家id
                String companyId = "companyId";
                if (searchMap.get(companyId) != null && !"".equals(searchMap.get(companyId))) {
                    predicateList.add(cb.like(root.get(companyId).as(String.class), "%" + (String) searchMap.get(companyId) + "%"));
                }
                // 店铺id
                String storeId = "storeId";
                if (searchMap.get(storeId) != null && !"".equals(searchMap.get(storeId))) {
                    predicateList.add(cb.like(root.get(storeId).as(String.class), "%" + (String) searchMap.get(storeId) + "%"));
                }
                // 店铺名称
                String storeName = "storeName";
                if (searchMap.get(storeName) != null && !"".equals(searchMap.get(storeName))) {
                    predicateList.add(cb.like(root.get(storeName).as(String.class), "%" + (String) searchMap.get(storeName) + "%"));
                }
                // 订单id
                String ordersId = "ordersId";
                if (searchMap.get(ordersId) != null && !"".equals(searchMap.get(ordersId))) {
                    predicateList.add(cb.like(root.get(ordersId).as(String.class), "%" + (String) searchMap.get(ordersId) + "%"));
                }
                //剔除软删除数据
                String deletedAt = "deletedAt";
                predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(LocalDateTime.class)));

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }


    /**
     * 根据companyId查商家下所有评价并分页
     *
     * @param companyId
     * @param page
     * @param size
     * @return
     */
    public Page<Evaluate> findAllByCompanyIdAndDeletedAtIsNullOrderByCreatedAtDesc(String companyId, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return evaluateDao.findAllByCompanyIdAndDeletedAtIsNullOrderByCreatedAtDesc(companyId, pageable);
    }

    /**
     * 根据storeId查找店铺下所有评价并分页
     *
     * @param storeId
     * @param page
     * @param size
     * @return
     */
    public Page<Evaluate> findAllByStoreIdAndDeletedAtIsNullOrderByCreatedAtDesc(String storeId, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return evaluateDao.findAllByStoreIdAndDeletedAtIsNullOrderByCreatedAtDesc(storeId, pageable);
    }

    /**
     * 根据memberID查找会员名下所有评价并分页
     *
     * @param memberId
     * @param page
     * @param size
     * @return
     */
    public Page<Evaluate> findAllByMemberIdAndDeletedAtIsNullOrderByCreatedAtDesc(String memberId, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return evaluateDao.findAllByMemberIdAndDeletedAtIsNullOrderByCreatedAtDesc(memberId, pageable);
    }

    /**
     * 根据id和memberID查找会员名下某一评价
     *
     * @param id
     * @param memberId
     * @return
     */
    public Evaluate findByIdAndMemberId(String id, String memberId) {
        return evaluateDao.findByIdAndMemberId(id, memberId);
    }

    /**
     * 根据ordersId和memberID查找会员名下某一评价
     *
     * @param ordersId
     * @param memberId
     * @return
     */
    public Evaluate findByOrdersIdAndMemberId(String ordersId, String memberId) {
        return evaluateDao.findByOrdersIdAndMemberId(ordersId, memberId);
    }
}
